package com.example.hyg_veintype.base;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.utils.AppManager;
import com.example.hyg_veintype.view.CustomProgressDialog;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_base.mainUtils.StatusBarUtil;



/**
 * @author: yyw
 * time: 2020/12/11 13:23
 * desc: 脉诊app activity基类
 */
public abstract class BaseVeinTypeActivity extends BaseActivity implements View.OnClickListener{

    protected RelativeLayout rlTop;//标题栏
    protected ImageView imgLeft;//左侧图片
    protected LinearLayout lnTopLeft;//左侧
    protected LinearLayout lnTopRight;//右侧
    protected TextView tvTitle;//标题
    protected ImageView imgRight;//右侧图片
    protected TextView tvRight;//右侧标题
    protected LinearLayout lnContent;//标题栏下面布局
    protected CustomProgressDialog progressDialog;//自定义的progressdialog

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.veintype_activity_base);
        StatusBarUtil.setStatusBar(1,this,getResources().getColor(R.color.white),00,null,null,true);

        imgLeft = findViewById(R.id.img_left);
        lnTopLeft = findViewById(R.id.ln_top_left);
        lnTopRight = findViewById(R.id.ln_top_right);
        tvTitle = findViewById(R.id.tv_title);
        imgRight = findViewById(R.id.img_right);
        tvRight = findViewById(R.id.tv_right);
        rlTop = findViewById(R.id.rl_top);
        lnContent = findViewById(R.id.ln_content);

        lnTopLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //设置内容的布局
        setLayout(getLayoutId());
        //activity管理
        AppManager.getAppManager().addActivity(this);
        //隐藏软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        //progressdialog
        progressDialog = CustomProgressDialog.createDialog(this);
        progressDialog.setCancelable(false);
    }

    /**
     * 设置内容的布局
     *
     * @param res
     */
    private void setLayout(int res) {
        LayoutInflater inflater = LayoutInflater.from(BaseVeinTypeActivity.this);
        View view = inflater.inflate(res, lnContent, true);
//        View view = inflater.inflate(res, null);
//        lnContent.addView(view);
        System.out.println("====");
    }

    /**
     * 内容布局
     *
     * @return
     */
    @LayoutRes
    protected abstract int getLayoutId();

    /**
     * 点击空白区域隐藏键盘.
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {  //把操作放在用户点击的时候
            View v = getCurrentFocus();      //得到当前页面的焦点,ps:有输入框的页面焦点一般会被输入框占据
            if (isShouldHideKeyboard(v, me)) { //判断用户点击的是否是输入框以外的区域
                hideKeyboard(v.getWindowToken());   //收起键盘
            }
        }
        return super.dispatchTouchEvent(me);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {  //判断得到的焦点控件是否包含EditText
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],    //得到输入框在屏幕中上下左右的位置
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击位置如果是EditText的区域，忽略它，不收起键盘。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略
        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     *
     * @param token
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().removeActivity(this.getClass());
    }
}
